package com.mls.fivedistrict.controller.portal;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.server.NewsNewsTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 新闻 新闻标签
 * Created by chenxiuxiang on 2018/6/27.
 */
@RestController("protalNewsNewsTagController")
@RequestMapping("/api/app")
public class NewsNewsTagController extends BaseController {
    @Autowired
    NewsNewsTagService newsNewsTagService;


    @GetMapping("/v1/tag/{tagId}/newses")
    public ServiceResponse getNewsByTag(@PathVariable Long tagId) {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        Wrapper wrapper = pageFilter.getWrapper();
        if (wrapper == null) {
            wrapper = Condition.create();
        }
        wrapper.eq("news_tag_id", tagId);
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = newsNewsTagService.selectList(wrapper);
        PageInfo pageInfo = new PageInfo(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);

    }

    @GetMapping("/v1/recommend/news/{newsId}")
    public ServiceResponse getRecommendNews(@PathVariable Long newsId) {
        List newsByRecommend = newsNewsTagService.getNewsByRecommend(newsId);
        PageInfo pageInfo = new PageInfo(newsByRecommend, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
}
